package com.alavente.dao;

import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alavente.objects.DemandeVente;

public abstract class HibernateAbstractDAO<T> implements GenericDAOInterface<T> {

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	private Class<T> entityClass;
	
	public List<T> getList(String hibTable) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<T> list = null;
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from " + hibTable).list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}
	
	public T getById(int id) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		T object = null;
		
		try {
			transaction = session.beginTransaction();			
			object = (T) session.get(object.getClass(), id);			
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return object;
	}

	
	public Boolean create(T object) {
		
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			session.save(object);
			transaction.commit();
		} 
		catch (HibernateException he) {
			if(transaction != null) {
				he.printStackTrace(); 
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
			return false;
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return true;
	}

	
	public Boolean update(T object) {
		
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			session.merge(object);
			transaction.commit();
		} 
		catch (HibernateException he) {
			if(transaction != null) {
				he.printStackTrace(); 
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
			
			return false;
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return true;
	}

	
	public Boolean delete(T obj) {

		return false;
	}
}
